package welcome

import (
    `database/sql`
    `net/http`
    `encoding/json`
    `strconv`
)

var DB *sql.DB

type Person struct {
    ID    int64
    Name  string
    Phone string
}

func longMethod(w http.ResponseWriter, r *http.Request) {
    err := r.ParseForm()
    if err != nil {
        w.WriteHeader(http.StatusPreconditionFailed)
        return
    }

    userID, err := strconv.ParseInt(r.Form.Get("UserID"), 10, 64)
    if err != nil {
        w.WriteHeader(http.StatusPreconditionFailed)
        return
    }

    row := DB.QueryRow("SELECT * FROM people WHERE ID = ?", userID)

    person := &Person{}
    err = row.Scan(&person.ID, &person.Name, &person.Phone)
    if err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        return
    }

    encoder := json.NewEncoder(w)
    err = encoder.Encode(person)
    if err != nil {
        w.WriteHeader(http.StatusInternalServerError)
        return
    }

}
